﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using common;
using System.Windows.Forms;
using Dapper;
using System.Data.SqlClient;
using DBEntity;
using DBClass;

namespace HTF_HMI
{
    public partial class AUDIT_PERIOD : Form
    {
        AutoSizeFormClass asc = new AutoSizeFormClass();

        public AUDIT_PERIOD()
        {
            InitializeComponent();             
        }

        private void AUDIT_PERIOD_EDIT_Load(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
            BindArea();
            btn_query_Click(null,null);          
        }



        private void BindArea()
        {
            var area = DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;         
        }

        public void btn_query_Click(object sender, EventArgs e)
        {
            if (cmb_task.SelectedValue == null)
            {
                bindingSource1.DataSource = DBHelper.GetAuditPeriod(cmb_area.Text, "");
            }
            else
            {
                bindingSource1.DataSource = DBHelper.GetAuditPeriod(cmb_area.Text, cmb_task.SelectedValue.ToString());
            }
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            cmb_area.SelectedIndex = 0;
            btn_query_Click(null,null);
        }  

        public void gridView3_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();
            
            if (rows.Count() == 1)
            {              
                string strDeviceId = this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["ID"]).ToString();             
            }           
        }

        private void btn_add_Click(object sender, EventArgs e)
        {            
            AUDIT_PERIOD_EDIT mpSheet = new AUDIT_PERIOD_EDIT(this, "");
            mpSheet.ShowDialog();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();           
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择要修改的巡检任务周期！");
                return;
            }
            if (rows.Count() == 1)
            {
                string strId = this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["ID"]).ToString();
                AUDIT_PERIOD_EDIT mpSheet = new AUDIT_PERIOD_EDIT(this, strId);
                mpSheet.ShowDialog();
            }

            if (rows.Count() > 1)
            {
                MessageBox.Show("请选择一条要修改的巡检任务周期！");
                return;
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择要删除的巡检项目！");
                return;
            }
            if (rows.Count() > 0)
            {
                List<int> ids = new List<int>();
                foreach (int row in rows)
                {
                    int strId = Int32.Parse(this.gridView3.GetRowCellValue(row, gridView3.Columns["ID"]).ToString());
                    ids.Add(strId);
                }
                bool result = DBHelper.DeleteAuditPeriod(ids);
                if (result)
                {
                    MessageBox.Show("删除成功！");
                   btn_query_Click(null, null);
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void cmb_area_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_task.ResetText();

            var task = DBHelper.GetAuditTask(cmb_area.Text);          
            cmb_task.DisplayMember = "TASK_NAME";
            cmb_task.ValueMember = "TASK_ID";
            cmb_task.DataSource = task;

            cmb_task.SelectedIndex = -1;             
        }
    }
}
